<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>YUI Browser History Manager - Calendar Example</title>
    <link rel="stylesheet" type="text/css" href="../../../build/reset-fonts-grids/reset-fonts-grids.css"/>
    <link rel="stylesheet" type="text/css" href="../../../build/calendar/assets/calendar.css">
    <link rel="stylesheet" type="text/css" href="../assets/solution.css"/>
    <script src="../../../build/yahoo/yahoo.js"></script>
    <script src="../../../build/event/event.js"></script>
    <script src="../../../build/dom/dom.js"></script>
    <script src="../../../build/calendar/calendar.js"></script>
    <script src="../../../build/history/history-experimental.js"></script>
  </head>
  <body>
    <script>

// The initial month will be chosen in the following order:
//
// URL fragment identifier (it will be there if the user previously
// bookmarked the application in a specific state)
//
//         or
//
// today's corresponding month (default)

var today = new Date();
var defaultCalendarState = ( today.getMonth() + 1 ) + "_" + today.getFullYear();
var bookmarkedCalendarState = YAHOO.util.History.getBookmarkedState( "calendar" );
var initialCalendarState = bookmarkedCalendarState || defaultCalendarState;

var calendar;

// Register our calendar module. Module registration MUST
// take place before calling YAHOO.util.History.initialize.
YAHOO.util.History.register( "calendar", initialCalendarState, function( state ) {
    // This is called after calling YAHOO.util.History.navigate, or after the user
    // has trigerred the back/forward button. We cannot discrminate between
    // these two situations.

    // Show the right month according to the "state" parameter:
    calendar.cfg.setProperty( "pagedate", state.replace( "_", "/" ) );
    calendar.render();
} );

// Subscribe to this event before calling YAHOO.util.History.initialize,
// or it may never get fired! Note that this is guaranteed to be fired
// after the window's onload event.
YAHOO.util.History.onLoadEvent.subscribe( function() {
    // This is the tricky part... The onLoad event is fired when the user
    // comes back to the page using the back button. In this case, the
    // actual month that needs to be loaded corresponds to the last month
    // visited before leaving the page, and not the initial month. This can
    // be retrieved using getCurrentState:
    var currentState = YAHOO.util.History.getCurrentState( "calendar" );
    var startDate = { pagedate: currentState.replace( "_", "/" ) };

    // Instantiate the calendar control...
    calendar = new YAHOO.widget.Calendar( "calendar", "calendarContainer", startDate );
    calendar.beforeRenderEvent.subscribe( handleCalendarBeforeRender, calendar, true );
    calendar.render();
} );

function handleCalendarBeforeRender() {
    var calDate = calendar.cfg.getProperty( "pageDate" );
    var pageDate = ( calDate.getMonth() + 1 ) + "_" + calDate.getFullYear();
    // The following test is crucial. Otherwise, we end up circling forever.
    // Indeed, YAHOO.util.History.navigate will call the module onStateChange callback,
    // which will call createCalendar, which will call calendar.render(), which will
    // end up calling handleCalendarBeforeRender, and it keeps going from here...
    var currentState = YAHOO.util.History.getCurrentState( "calendar" );
    if ( pageDate != currentState )
        YAHOO.util.History.navigate( "calendar", pageDate );
}

// The call to YAHOO.util.History.initialize should ALWAYS be from within
// a script block located RIGHT AFTER the body tag (this seems to prevent
// an edge case bug on IE - IE seems to sometimes forget the history when
// coming back to a page, and the back - or forward button depending on the
// situation - is disabled...)
YAHOO.util.History.initialize( "../../../build/history/assets/blank.html" );

    </script>
    <div id="doc">
      <div id="hd">
        <img src="../assets/yui.gif" alt="YUI Logo" id="logo"/>
      </div>
      <div id="bd">
        <div id="calendarContainer"></div>
      </div>
      <div id="ft">YUI Browser History Manager - Calendar Example</div>
    </div>
  </body>
</html>
